﻿function resizeImage(o, Mw, Mh, need_margin) {
    var _Mw = Mw || document.documentElement.clientWidth;
    var _Mh = Mh || document.documentElement.clientHeight;
    var _image = new Image();
    _image.src = o.src;
    function getRightWH(Rw, Rh, Mw, Mh) {
        var index = 0, _Rw = Rw, _Rh = Rh;
        if (Rw < Mw) {
            index += 1;
        }
        if (Rh < Mh) {
            index += 2;
        }
        switch (index) {
            case 0: _Rw = (Rh / Mh > Rw / Mw) ? Mw : Rw * Mh / Rh; _Rh = (Rh / Mh < Rw / Mw) ? Mh : Rh * Mw / Rw; break;
            case 1: _Rw = Mw; _Rh = Rh * Mw / Rw; break;
            case 2: _Rh = Mh; _Rw = Rw * Mh / Rh; break;
            case 3: _Rw = (Rh / Mh > Rw / Mw) ? Mw : Rw / Rh * Mh; _Rh = (Rh / Mh > Rw / Mw) ? Mh : Mh / Rw * Mw; break;
        }
        return [_Rw, _Rh];
    }
    var wh = getRightWH(_image.width, _image.height, _Mw, _Mh);
    o.style.width = wh[0] + 'px';
    o.style.height = wh[1] + 'px';
    o.style.visibility = 'visible';
    if (need_margin) {
        o.style.marginTop = (Mh - parseInt(wh[1], 10)) / 2 + 'px';
    }
    _image = null;
    return true;
}